$(function() {
	var name = $('.name');
	var Yname = $('.Yname');
	var ceshiIndex = 0;
	var gundongBoss,gundongY
	var fuliArr = [];
	fuliArr.length = 10;
	var useBossArr = [],usePersonsArr = []
	var rowHtml = '';
	for (var n=1;n<=10;n++) {
		if (!localStorage.getItem('fuliArr2')) {
			
		} else {
			fuliArr = JSON.parse(localStorage.getItem('fuliArr2'))
		}
		if (!fuliArr[n-1]) {
			rowHtml += '<div class="row" index="'+(n-1)+'"><div>福利'+n+'</div></div>'
		} else {
			rowHtml += '<div class="row" index="'+(n-1)+'">'+fuliArr[(n-1)]+'</div>'
		}
		/*if (!localStorage.getItem('fuliIndex'+(n-1))) {
			rowHtml += '<div class="row" index="'+(n-1)+'"><div>福利'+n+'</div></div>'
		} else {
			rowHtml += '<div class="row" index="'+(n-1)+'">'+fuli[(n-1)].desc+'</div>'
		}*/
	}
	$('.right_bg').html(rowHtml)
	localStorage.setItem('originBoss2',JSON.stringify(bossArray) )
	localStorage.setItem('originPersons2',JSON.stringify(personArray))
	if (!localStorage.getItem('useBoss2')) {
		useBossArr = bossArray
	} else {
		useBossArr = JSON.parse(localStorage.getItem('useBoss2'))
	}
	if (!localStorage.getItem('usePersons2')) {
		usePersonsArr = personArray
	} else {
		usePersonsArr = JSON.parse(localStorage.getItem('usePersons2'))
	}
	//循环参加名单
	function startNum() {
		// 时时更新pcount人数
		var pcount = useBossArr.length - 1;
		var num = Math.floor(Math.random() * (pcount-0) + 0);
		ceshiIndex++;
		if (ceshiIndex == 5) {
			ceshiIndex = 0;
			num = pcount;
		}
		if (useBossArr.length <= 2) {
			num = Math.round(Math.random())
		}
		if (useBossArr.length == 1) {
			num = 0
		}
		name.html(useBossArr[num].name).attr('bossIndex',useBossArr[num].id);
		gundongBoss = setTimeout(startNum, 50);
	}
	//startNum()
	$('.bossBtn').click(function() {
		if (useBossArr.length == 0) {
			alert('所有老板已抽取完，请双击新未来，大征程图片重置')
			return;
		}
		if ($('.bossBtn img').attr('src').indexOf('boss') >= 0) {
			$('.bossBtn img').attr('src','img/stopBtn.png')
			startNum()
		} else {
			$('.bossBtn img').attr('src','img/bossBtn.png')
			useBossArr.forEach(function(bossItem,index) {
				if (bossItem.id == (name.attr('bossIndex') || '')) {
					useBossArr.splice(index,1)
					localStorage.setItem('getLunck'+new Date(),JSON.stringify(bossItem))
				}
			})
			localStorage.setItem('useBoss2',JSON.stringify(useBossArr))
			clearTimeout(gundongBoss)
		}
	})
	function startYNum() {
		// 时时更新pcount人数
		var pcount = usePersonsArr.length - 1;
		var num = Math.floor(Math.random() * pcount);
		console.log(usePersonsArr[num])
		Yname.html(usePersonsArr[num].address+' '+ usePersonsArr[num].name).attr('personIndex',usePersonsArr[num].id);
		gundongY = setTimeout(startYNum, 50);
	}
	//startNum()
	$('.YBtn').click(function() {
		if ($('.YBtn img').attr('src').indexOf('user') >= 0) {
			$('.YBtn img').attr('src','img/stopBtn.png')
			startYNum()
		} else {
			$('.YBtn img').attr('src','img/userBtn.png')
			usePersonsArr.forEach(function(personItem,personIndex) {
				if (personItem.id == (Yname.attr('personIndex') || '')) {
					usePersonsArr.splice(personIndex,1)
					localStorage.setItem('getLunckY'+new Date(),JSON.stringify(personItem))
				}
			})
			localStorage.setItem('usePersons2',JSON.stringify(usePersonsArr))
			clearTimeout(gundongY)
		}
	})
	$('.row div').click(function() {
		var that = this;
		var index = 10 - $('.row div').length;
		var num = $(that).parent().attr('index');
		console.log(num)
		/*var index = $(this).parent().attr('index')*/
		/*console.log(index)
		localStorage.setItem('fuliIndex'+$(this).parent().attr('index'),$(this).parent().attr('index'))*/
		$('body').css('overflow','hidden')
		$(this).addClass('animated').addClass('rotateOutUpRight');
		setTimeout(function() {
			$(that).parent().text(fuli[index].desc).css('color','white')
			fuliArr[num] = fuli[index].desc;
			localStorage.setItem('fuliArr2',JSON.stringify(fuliArr))
			$('body').css('overflow','auto')
		},1000)
	})
	$('#resetData').dblclick(function() {
		if (confirm('确定重置老板的信息吗？')) {
    		localStorage.removeItem('useBoss2')
    		location.reload();
    	} 
	})
	$('#open').dblclick(function() {
		if (confirm('确定重置员工的信息吗？')) {
    		localStorage.removeItem('usePersons2')
    		location.reload();
    	} 
	})
	$('#logo').dblclick(function() {
		if (confirm('确定重置全部信息吗？')) {
    		localStorage.clear()
    		location.reload();
    	} 
	})
})
